"""
Case Type   : 功能测试
Case Name   : 集群带业务，执行switchover后stop原主节点
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.拉起TPCC业务
./runBenchmark.sh pri_props.og
./runBenchmark.sh slave_props.og
    2.备2升主
cm_ctl switchover -n 3 -D $PGDATA
    3.查看业务
    4.stop主节点
cm_ctl stop -n 1
    5.每个节点查看集群控制文件
pg_controldata +data
    6.恢复环境
cm_ctl start
Expect      :
    1.成功
    2.成功
    3.正常
    4.成功
    5.in production
    6.成功
History     :
"""

import unittest
import os
import time
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro
logger = Logger()

class Test_Xlog_Syncretism_0011(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.pri_node = Node('PrimaryDbUser')
        self.sta1_node = Node('Standby1DbUser')
        self.sta2_node = Node('Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.tpcc_node = Node('Standby2Root')
        self.constant = Constant()
        self.com = Common()
        self.pri_tpcc_log = 'pri_tpcc_result.log'
        self.sta_tpcc_log = 'sta_tpcc_result.log'
    
    def test_shared_storage(self):
        text = '-----step1:拉起TPCC业务 expect:成功-----'
        logger.info(text)
        logger.info(text)
        logger.info('-----主节点读写业务-----')
        pri_tpcc = ComThread(self.com.startTPCC(), args=(self.tpcc_node, 
            f'{macro.PRI_PROPS_30}', self.pri_tpcc_log))
        pri_tpcc.setDaemon(True)
        pri_tpcc.start()
        logger.info('-----备节点读写业务-----')
        sta_tpcc = ComThread(self.com.startTPCC(), args=(self.tpcc_node, 
            f'{macro.STA_PROPS_30}', self.sta_tpcc_log))
        sta_tpcc.setDaemon(True)
        sta_tpcc.start()

        text = '-----step2:备2升主 expect:成功-----'
        logger.info(text)
        res = self.pri_sh.exec_cm_ctl('switchover', f'-n 3 -D {macro.DB_INSTANCE_PATH}')
        logger.info(res)
        self.assertIn(self.constant.cm_switchover_success_msg, res)

        text = '-----step3:查看业务 expect:成功-----'
        time.sleep(5)
        pri_cmd = f'cd {macro.TPCC_PATH} && tail -n 1 {self.pri_tpcc_log}'
        sta_cmd = f'cd {macro.TPCC_PATH} && tail -n 1 {self.sta_tpcc_log}'
        logger.info(pri_cmd)
        pri_res = self.tpcc_node.sh(pri_cmd).result()
        logger.info(pri_res)
        self.assertIn('Current tmpTOTAL', pri_res, 'tpcc业务执行失败')
        logger.info(sta_cmd)
        sta_res = self.tpcc_node.sh(sta_cmd).result()
        logger.info(sta_res)
        self.assertIn('Current tmpTOTAL', pri_res, 'tpcc业务执行失败')

        text = '-----step4:stop原主节点 expect:成功-----'
        logger.info(text)
        res = self.pri_sh.exec_cm_ctl('stop', f'-n 1 -D {macro.DB_INSTANCE_PATH}')
        logger.info(res)
        self.assertIn(self.constant.cm_stop_instance_success_msg, res)

        text = '-----step5:查看集群控制文件 expect:成功-----'
        while True:
            status = self.pri_sh.exec_cm_ctl('query', '-Cv')
            if "Primary Normal" in status and 'Down    Manually stopped' in status:
                logger.info(status)
                break
            time.sleep(5)
        logger.info('-----主节点校验-----')
        cmd = f'source {macro.DB_ENV_PATH}; pg_controldata +data'
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        self.assertTrue('' == res, '主节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd)
        res = self.sta1_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               in production', 
            res, '备1节点校验失败')
        logger.info('-----备2节点校验-----')
        logger.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               in production', 
            res, '备2节点校验失败')
    
    def tearDown(self):
        text = '-----step6:恢复环境 expect:成功-----'
        logger.info(text)
        logger.info('-----停tpcc线程-----')
        pri_tpcc = self.com.get_pid(self.tpcc_node, f"ps -ux | grep '{macro.PRI_PROPS_30}'" \
            f" | grep -v '--color")
        sta_tpcc = self.com.get_pid(self.tpcc_node, f"ps -ux | grep '{macro.STA_PROPS_30}'" \
            f" | grep -v '--color")
        logger.info(pri_tpcc)
        cmd = f"kill -9 {pri_tpcc}"
        logger.info(cmd)
        res = self.tpcc_node.sh(cmd).result()
        logger.info(res)
        logger.info(sta_tpcc)
        cmd = f"kill -9 {sta_tpcc}"
        logger.info(cmd)
        res = self.tpcc_node.sh(cmd).result()
        logger.info(res)
        res = self.pri_sh.exec_cm_ctl('start', '-n 1')
        logger.info(res)
        self.assertIn(self.constant.cm_start_node_success_msg, res)
        res = self.pri_sh.exec_cm_ctl('switchover', '-a')
        logger.info(res)
        self.assertIn(self.constant.cm_switchover_success_msg, res)
        logger.info(f'-----{os.path.basename(__file__)} end-----')